#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStackedWidget>
#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QScrollArea>
#include <QGroupBox>
#include <QRadioButton>
#include <QButtonGroup>
#include <QTextEdit>
#include <QVector>
#include <QPair>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QFile>
#include <QString>
#include <QProgressBar>

// Forward declarations for pages
class WelcomePage;
class QuestionnairePage;
class ResultsPage;
class TraitMatchGamePage; // Added
class WhichScenarioGamePage; // Added

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    struct MBTIQuestion {
        int id;
        QString text;
        QString option_a_text;
        QString option_b_text;
        QString option_a_scores_pole;
        QString option_b_scores_pole;
    };

private slots:
    void startTest();
    void submitTest(const QVector<int>& answers);
    void restartTest();
    void showTraitMatchGame(); // Added
    void showWhichScenarioGame(); // Added
    void showWelcomePage(); // Added
    
private:
    void setupUi();
    void loadMBTIQuestions(); 
    void loadMBTIDatabase();
    QString calculateMBTI(const QVector<int>& answers); 
    QJsonObject getMBTIDetails(const QString& mbtiType);

    QStackedWidget *stackedWidget;
    WelcomePage *welcomePage;
    QuestionnairePage *questionnairePage;
    ResultsPage *resultsPage;
    TraitMatchGamePage *traitMatchGamePage; // Added
    WhichScenarioGamePage *whichScenarioGamePage; // Added

    QVector<MBTIQuestion> questionsList; 
    QJsonArray mbtiDatabase; 
    QString dataPath;
};

// Welcome Page
class WelcomePage : public QWidget
{
    Q_OBJECT
public:
    WelcomePage(QWidget *parent = nullptr);
    QPushButton *startButton;
    QPushButton *traitMatchGameButton; // Added
    QPushButton *whichScenarioGameButton; // Added
    QLabel* titleLabel;
    QLabel* introLabel;
};

// Questionnaire Page
class QuestionnairePage : public QWidget
{
    Q_OBJECT
public:
    QuestionnairePage(const QVector<MainWindow::MBTIQuestion>& questions, QWidget *parent = nullptr);
    QVector<int> getAnswers(); 

signals:
    void testSubmitted(const QVector<int>& answers);

private slots:
    void handleSubmit();
    void updateProgress();

private:
    QVBoxLayout *questionsLayout;
    QVector<QButtonGroup*> answerGroups;
    QPushButton *submitButton;
    QScrollArea *scrollArea;
    const QVector<MainWindow::MBTIQuestion>& questionsData;
    QProgressBar *progressBar;
    QLabel *progressLabel;
};

// Results Page
class ResultsPage : public QWidget
{
    Q_OBJECT
public:
    ResultsPage(QWidget *parent = nullptr);
    void setResult(const QString& mbtiType, const QJsonObject& details, const QPixmap& avatarPixmap);
    QPushButton *restartButton;

private:
    QLabel *resultTypeLabel;
    QLabel *avatarLabel; // For displaying MBTI avatar
    QTextEdit *detailsTextEdit;
    QLabel *celebritiesTitleLabel;
    QTextEdit *celebritiesTextEdit;
    QLabel *strengthsTitleLabel;
    QTextEdit *strengthsTextEdit;
    QLabel *weaknessesTitleLabel;
    QTextEdit *weaknessesTextEdit;
    QLabel *sloganLabel;

};

#endif // MAINWINDOW_H

